from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.platypus import Table, TableStyle
from reportlab.lib import colors

def create_invoice_template():
    pdf_path = "invoice_template.pdf"
    pdf_canvas = canvas.Canvas(pdf_path, pagesize=A4)

    # 设置页面标题
    pdf_canvas.setFont("Helvetica-Bold", 16)
    pdf_canvas.drawString(220, 800, "Invoice")

    # 公司信息
    pdf_canvas.setFont("Helvetica", 12)
    pdf_canvas.drawString(50, 780, "Company Name: XYZ Ltd.")
    pdf_canvas.drawString(50, 765, "Address: 123 Example St., City")
    pdf_canvas.drawString(50, 750, "Phone: (123) 456-7890")
    pdf_canvas.drawString(50, 735, "Email: contact@xyz.com")

    # 客户信息部分
    pdf_canvas.drawString(50, 700, "Bill To:")
    pdf_canvas.drawString(50, 685, "Customer Name:")
    pdf_canvas.drawString(50, 670, "Customer Address:")

    # 添加表格表头
    data = [["Item", "Description", "Quantity", "Unit Price", "Total"]]
    table = Table(data)
    table.setStyle(TableStyle([
        ("BACKGROUND", (0, 0), (-1, 0), colors.grey),
        ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
        ("ALIGN", (0, 0), (-1, -1), "CENTER"),
        ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
        ("FONTSIZE", (0, 0), (-1, 0), 12),
        ("BOTTOMPADDING", (0, 0), (-1, 0), 12),
        ("GRID", (0, 0), (-1, -1), 0.5, colors.black),
    ]))
    table.wrapOn(pdf_canvas, 450, 400)
    table.drawOn(pdf_canvas, 50, 600)

    # 保存模板
    pdf_canvas.save()
    print(f"Invoice template saved as {pdf_path}")

# 生成模板
create_invoice_template()
